import type { App } from "vue";
import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },

  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        // 用于 keep-alive 功能，需要与 SFC 中自动推导或显式声明的组件名称一致
        // 参考文档: https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
        name: "Dashboard",
        meta: {
          title: "dashboard",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },
      {
        path: "401",
        component: () => import("@/views/error/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error/404.vue"),
        meta: { hidden: true },
      },
      {
        path: "profile",
        name: "Profile",
        component: () => import("@/views/profile/index.vue"),
        meta: { title: "个人中心", icon: "user", hidden: true },
      },
      {
        path: "myNotice",
        name: "MyNotice",
        component: () => import("@/views/system/notice/components/MyNotice.vue"),
        meta: { title: "我的通知", icon: "user", hidden: true },
      },
    ],
  },

  // 游戏路由
  {
    path: "/game",
    component: Layout,
    redirect: "/game/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/game/index.vue"),
        name: "Game",
        meta: {
          title: "游戏中心",
          icon: "/src/assets/images/游戏中心.png",
          keepAlive: true,
        },
      },
    ],
  },

  // 通知路由
  {
    path: "/inform",
    component: Layout,
    redirect: "/inform/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/Inform/index.vue"),
        name: "Inform",
        meta: {
          title: "通知中心",
          icon: "/src/assets/images/通知.png",
          keepAlive: true,
        },
      },
    ],
  },

  // 排行榜路由
  {
    path: "/leaderboard",
    component: Layout,
    redirect: "/leaderboard/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/leaderboard/index.vue"),
        name: "Leaderboard",
        meta: {
          title: "排行榜",
          icon: "/src/assets/images/排行榜.png",
          keepAlive: true,
        },
      },
    ],
  },

  // 社交功能路由 - 合并为单个页面
  {
    path: "/social",
    component: Layout,
    redirect: "/social/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/friend/social.vue"),
        name: "Social",
        meta: {
          title: "社交中心",
          icon: "/src/assets/images/社交.png",
          keepAlive: true,
        },
      },
    ],
  },

  // 用户信息路由
  {
    path: "/userinfo",
    component: Layout,
    redirect: "/userinfo/index",
    name: "UserInfo",
    meta: {
      title: "用户管理",
      icon: "/src/assets/images/用户管理.png",
    },
    children: [
      {
        path: "list",
        component: () => import("@/views/userinfo/list.vue"),
        name: "UserList",
        meta: {
          title: "用户列表",
          keepAlive: true,
        },
      },
      {
        path: "index/:id?",
        component: () => import("@/views/userinfo/index.vue"),
        name: "UserDetail",
        meta: {
          title: "用户详情",
          keepAlive: true,
        },
      },
      {
        path: "statistics/:id?",
        component: () => import("@/views/userinfo/statistics.vue"),
        name: "UserStatistics",
        meta: {
          title: "用户统计",
          keepAlive: true,
        },
      },
    ],
  },

  // 以下路由需要隐藏
  // 代码生成路由
  {
    path: "/codegen",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "index",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Codegen",
        meta: {
          title: "代码生成",
          icon: "code",
          hidden: true,
        },
      },
    ],
  },

  // 接口文档路由
  {
    path: "/api",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "apifox",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Apifox",
        meta: {
          title: "接口文档",
          icon: "api",
          hidden: true,
        },
      },
    ],
  },

  // 平台文档路由
  {
    path: "/doc",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "index",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Doc",
        meta: {
          title: "平台文档",
          icon: "document",
          hidden: true,
        },
      },
    ],
  },

  // 多级菜单路由
  {
    path: "/nested",
    component: Layout,
    meta: { hidden: true },
    name: "Nested",
    children: [
      {
        path: "menu1",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Menu1",
        meta: {
          title: "多级菜单",
          hidden: true,
        },
      },
    ],
  },

  // 组件封装路由
  {
    path: "/component",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "index",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Components",
        meta: {
          title: "组件封装",
          hidden: true,
        },
      },
    ],
  },

  // 路由参数
  {
    path: "/route",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "param/:id",
        component: () => import("@/views/dashboard/index.vue"),
        name: "RouteParam",
        meta: {
          title: "路由参数",
          hidden: true,
        },
      },
    ],
  },

  // 功能演示
  {
    path: "/demo",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "index",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Demo",
        meta: {
          title: "功能演示",
          hidden: true,
        },
      },
    ],
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

export default router;
